#pragma once
#include <boost/bind.hpp>
class CNameCreateBase;
typedef boost::function<CNameCreateBase*()> ParamCreateFunction;
typedef boost::function<int(const ZMVector<SharedPointer<CNameCreateBase>>&)> RunFunction;

class CObjectCreateFromName
{
public:
	CObjectCreateFromName();
	~CObjectCreateFromName();
	//static SharedPointer<CObjectCreateFromName>  GetInstance();
	static CObjectCreateFromName*  GetInstance();

	bool HasParam(const QString& str) const
	{
		return m_paramGroup.constFind(str) != m_paramGroup.constEnd();
	}

	void AddParam(const QString& str, ParamCreateFunction func)
	{
		if (!HasParam(str))
		{
			m_paramGroup[str] = func;
		}
	}

	void AddFunction(const QString& str, RunFunction func)
	{
		m_fucGroup[str] = func;
	}

	CNameCreateBase* CreatObjFromName(QString strName, QString strPreName = "")
	{
		CNameCreateBase* ret = NULL;
		QMap<QString, ParamCreateFunction>::iterator pos = m_paramGroup.find(strPreName + strName);
		if (pos != m_paramGroup.end())
		{
			ret = pos.value()();
		}
		return ret;
	}

	RunFunction CreatFuncFromName(QString strName)
	{
		RunFunction ret = NULL;
		QMap<QString, RunFunction>::iterator pos = m_fucGroup.find(strName);
		if (pos != m_fucGroup.end())
		{
			ret = pos.value();
		}
		assert(ret != NULL);
		return ret;
	}
private:
	//static SharedPointer<CObjectCreateFromName> m_pCreate;
	static CObjectCreateFromName* m_pCreate;
	QMap<QString, ParamCreateFunction> m_paramGroup;
	QMap<QString, RunFunction> m_fucGroup;
};

